/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import Interface.IFile;
import InterfaceClient.JDriveCombo;
import Packets.pkValue;
import StaticClass.ArrSocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.swing.JOptionPane;
import org.omg.CORBA.INITIALIZE;

/**
 *
 * @author Oho
 */
public class ThreadFile extends Thread {

    Socket clientSocket;

    ThreadFile(Socket s) {
        //try {
        //   ArrSocket.ArrFile.add(this);
        clientSocket = s;


    }
    IFile file;
    //JDriveCombo cbb =null;

    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if (inp == null) {
            inp = new ObjectInputStream(clientSocket.getInputStream());
        }
        //pkValue pk = (pkValue) inp.readObject();
        String name = inp.readUTF();
        long size = inp.readLong();
        int bytesRead;
        FileOutputStream output = new FileOutputStream(URLAcc + "/" + name);
        byte[] buffer = new byte[1024];
        while (size > 0 && (bytesRead = inp.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
            output.write(buffer, 0, bytesRead);
            size -= bytesRead;
        }
        output.flush();
        output.close();
        JOptionPane.showMessageDialog(null, "Nhận file thành công");
        File fi = new File(URLAcc);

        file.initGUI(fi.getPath());

    }

    @Override
    public void run() {
        ArrSocket.ArrFile.add(this);
        while (true) {
            try {
                ThreadRead();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    // ObjectOutputStream outp = null;
    ObjectInputStream inp = null;
    ObjectOutputStream dos;
    String URLAcc = "";
    String URLClient = "";

    public void ThreadAccess(String URLFile[], String URLAcc, IFile f) {
        this.file = f;
        try {
            if (dos == null) {
                try {
                    dos = new ObjectOutputStream(clientSocket.getOutputStream());
                } catch (IOException ex) {
                    Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.URLAcc = URLAcc;
            this.URLClient = URLFile[0];
            pkValue vl = new pkValue(2, 2, URLFile);

            dos.writeObject(vl);
            dos.flush();

        } catch (IOException ex) {
            Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ThreadWrite1(String text, String URLClient) throws IOException {

        File f = new File(text);
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
//
        DataInputStream dis = new DataInputStream(bis);


        OutputStream os = clientSocket.getOutputStream();
        dos = new ObjectOutputStream(os);
        pkValue vl = new pkValue(2, 2, "GUIFILE");

        dos.writeObject(vl);
        dos.flush();
        //Sending size of file.
        // DataOutputStream dos = new DataOutputStream(os);
//        dos.writeUTF(clientSocket+);
        dos.writeUTF(URLClient + "/" + f.getName());
        dos.writeLong(f.length());
        byte[] arr = new byte[1024];
        try {
            int len = 0;
            while ((len = dis.read(arr)) != -1) {
                dos.write(arr, 0, len);

            }

        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex);

        }
        JOptionPane.showMessageDialog(null, "Gửi File Thành Công !!");
        fis.close();
        bis.close();
        dis.close();
        os.flush();

        dos.flush();

        //   socket.close();
//        outStream.writeObject("SEND_COMPLETE"); 

        //      bis.read(mybytearray, 0, mybytearray.length);
        // goi ten file 

        // goi duong dan luu file lai client

//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(URLClient);
//        outp.writeObject(pk);
        //goi du lieu 
//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(mybytearray);
//        outp.writeObject(pk);
        // outp.flush();
    }
//    public void ThreadWrite(String text,String URLClient) throws IOException {
//        if (outp == null) {
//            outp = new ObjectOutputStream(clientSocket.getOutputStream());
//        }
//        File myFile = new File(text);
//        byte[] mybytearray = new byte[(int) myFile.length()];
//        FileInputStream fis = new FileInputStream(myFile);
//        BufferedInputStream bis = new BufferedInputStream(fis);
//        bis.read(mybytearray, 0, mybytearray.length);
//        // goi ten file 
//        pkValue pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(URLClient+myFile.getName());
//        outp.writeObject(pk);
//        // goi duong dan luu file lai client
//        
////        pk = new pkValue();
////        pk.setCommand(2);
////        pk.setValue(1);
////        pk.setObj(URLClient);
////        outp.writeObject(pk);
//        //goi du lieu 
//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(mybytearray);
//        outp.writeObject(pk);
//        outp.flush();
//    }
}
